package com.dotstone.chipism.ui.dialog;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dotstone.chipism.R;
import com.dotstone.chipism.ui.timing.AddSocketTiming1Activity;
import com.dotstone.chipism.ui.timing.util.ShareParam;


public class SelectRemindWayPopup implements OnClickListener {
    private TextView remaind_way1, remaind_way2;
    public PopupWindow mPopupWindow;
    private SelectRemindWayPopupOnClickListener selectRemindWayPopupListener;
    private LinearLayout once_ll;
    private LinearLayout day_ll;
    private LinearLayout workday_ll;
    private LinearLayout weekend_ll;
    private LinearLayout diy_ll;
    private ImageView once_iv;
    private ImageView day_iv;
    private ImageView workday_iv;
    private ImageView weekend_iv;
    private ImageView diy_iv;
    private int value;
    View view ;

    public PopupWindow getmPopupWindow() {
        return mPopupWindow;
    }

    private Context mContext;

    @SuppressWarnings("deprecation")
    public SelectRemindWayPopup(Context context) {
        mContext = context;
        mPopupWindow = new PopupWindow(context);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        mPopupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
        mPopupWindow.setHeight(WindowManager.LayoutParams.FILL_PARENT);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setAnimationStyle(R.style.AnimBottom);
        mPopupWindow.setContentView(initViews());

        mPopupWindow.getContentView().setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mPopupWindow.setFocusable(false);
                mPopupWindow.dismiss();
                return true;
            }
        });

    }

    public View initViews() {
       view = LayoutInflater.from(mContext).inflate(R.layout.selectremindway_pop_window,
                null);

        once_ll = (LinearLayout) view.findViewById(R.id.once_ll);
        day_ll = (LinearLayout) view.findViewById(R.id.day_ll);
        workday_ll = (LinearLayout) view.findViewById(R.id.workday_ll);
        weekend_ll = (LinearLayout) view.findViewById(R.id.weekend_ll);
        diy_ll = (LinearLayout) view.findViewById(R.id.diy_ll);

        once_iv = (ImageView) view.findViewById(R.id.once_iv);
        day_iv = (ImageView) view.findViewById(R.id.day_iv);
        workday_iv = (ImageView) view.findViewById(R.id.workday_iv);
        weekend_iv = (ImageView) view.findViewById(R.id.weekend_iv);
        diy_iv = (ImageView) view.findViewById(R.id.diy_iv);

        value = ShareParam.getIntParam(mContext, "value");
        if (value == 0) {
            once_iv.setVisibility(View.VISIBLE);
            day_iv.setVisibility(View.INVISIBLE);
            workday_iv.setVisibility(View.INVISIBLE);
            weekend_iv.setVisibility(View.INVISIBLE);
            diy_iv.setVisibility(View.INVISIBLE);
        } else if (value == 1) {
            once_iv.setVisibility(View.INVISIBLE);
            day_iv.setVisibility(View.VISIBLE);
            workday_iv.setVisibility(View.INVISIBLE);
            weekend_iv.setVisibility(View.INVISIBLE);
            diy_iv.setVisibility(View.INVISIBLE);
        } else if (value ==2) {
            once_iv.setVisibility(View.INVISIBLE);
            day_iv.setVisibility(View.INVISIBLE);
            workday_iv.setVisibility(View.VISIBLE);
            weekend_iv.setVisibility(View.INVISIBLE);
            diy_iv.setVisibility(View.INVISIBLE);
        }else if (value ==3) {
            once_iv.setVisibility(View.INVISIBLE);
            day_iv.setVisibility(View.INVISIBLE);
            workday_iv.setVisibility(View.INVISIBLE);
            weekend_iv.setVisibility(View.VISIBLE);
            diy_iv.setVisibility(View.INVISIBLE);
        }else if (value ==4) {
            once_iv.setVisibility(View.INVISIBLE);
            day_iv.setVisibility(View.INVISIBLE);
            workday_iv.setVisibility(View.INVISIBLE);
            weekend_iv.setVisibility(View.INVISIBLE);
            diy_iv.setVisibility(View.VISIBLE);
        }
        once_ll.setOnClickListener(this);
        day_ll.setOnClickListener(this);
        workday_ll.setOnClickListener(this);
        weekend_ll.setOnClickListener(this);
        diy_ll.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.once_ll:
                selectRemindWayPopupListener.obtainMessage(0);
                ShareParam.putIntParam(mContext,"value",0);
                ShareParam.putStringParam(mContext,"name","执行一次");
                dismiss();
                break;
            case R.id.day_ll:
                selectRemindWayPopupListener.obtainMessage(1);
                ShareParam.putIntParam(mContext,"value",1);
                ShareParam.putStringParam(mContext,"name","每天");
                dismiss();
                break;
            case R.id.workday_ll:
                selectRemindWayPopupListener.obtainMessage(2);
                ShareParam.putIntParam(mContext,"value",2);
                ShareParam.putStringParam(mContext,"name","工作日");
                dismiss();
                break;
            case R.id.weekend_ll:
                selectRemindWayPopupListener.obtainMessage(3);
                ShareParam.putIntParam(mContext,"value",3);
                ShareParam.putStringParam(mContext,"name","周末");
                dismiss();
                break;
            case R.id.diy_ll:
              //  selectRemindWayPopupListener.obtainMessage(4);

               // ShareParam.putIntParam(mContext,"value",4);
                selectRemindCycle();
                break;
            default:
                break;
        }


    }

    public void selectRemindCycle() {
        final SelectRemindCyclePopup fp = new SelectRemindCyclePopup(mContext);
        fp.showPopup(AddSocketTiming1Activity.tvTitle);
        fp.setOnSelectRemindCyclePopupListener(new SelectRemindCyclePopup
                .SelectRemindCyclePopupOnClickListener() {

            @Override
            public void obtainMessage(int flag, String ret) {
                switch (flag) {
                    // 星期一
                    case 0:

                        break;
                    // 星期二
                    case 1:

                        break;
                    // 星期三
                    case 2:

                        break;
                    // 星期四
                    case 3:

                        break;
                    // 星期五
                    case 4:

                        break;
                    // 星期六
                    case 5:

                        break;
                    // 星期日
                    case 6:

                        break;
                    // 确定
                    case 7:
                        int repeat = Integer.valueOf(ret);
                        AddSocketTiming1Activity.workday_tv.setText(parseRepeat(repeat, 0));
                       // cycle = repeat;
                        ShareParam.putIntParam(mContext,"value",4);
                        ShareParam.putStringParam(mContext,"name",parseRepeat(repeat, 0));
                        fp.dismiss();
                        dismiss();
                        break;

                    default:
                        break;
                }
            }
        });
    }
    /**
     * @param repeat 解析二进制闹钟周期
     * @param flag   flag=0返回带有汉字的周一，周二cycle等，flag=1,返回weeks(1,2,3)
     * @return
     */
    public static String parseRepeat(int repeat, int flag) {
        String cycle = "";
        String weeks = "";
        if (repeat == 0) {
            repeat = 127;
        }
        if (repeat % 2 == 1) {
            cycle = "周一";
            weeks = "1";
        }
        if (repeat % 4 >= 2) {
            if ("".equals(cycle)) {
                cycle = "周二";
                weeks = "2";
            } else {
                cycle = cycle + "," + "周二";
                weeks = weeks + "," + "2";
            }
        }
        if (repeat % 8 >= 4) {
            if ("".equals(cycle)) {
                cycle = "周三";
                weeks = "3";
            } else {
                cycle = cycle + "," + "周三";
                weeks = weeks + "," + "3";
            }
        }
        if (repeat % 16 >= 8) {
            if ("".equals(cycle)) {
                cycle = "周四";
                weeks = "4";
            } else {
                cycle = cycle + "," + "周四";
                weeks = weeks + "," + "4";
            }
        }
        if (repeat % 32 >= 16) {
            if ("".equals(cycle)) {
                cycle = "周五";
                weeks = "5";
            } else {
                cycle = cycle + "," + "周五";
                weeks = weeks + "," + "5";
            }
        }
        if (repeat % 64 >= 32) {
            if ("".equals(cycle)) {
                cycle = "周六";
                weeks = "6";
            } else {
                cycle = cycle + "," + "周六";
                weeks = weeks + "," + "6";
            }
        }
        if (repeat / 64 == 1) {
            if ("".equals(cycle)) {
                cycle = "周日";
                weeks = "7";
            } else {
                cycle = cycle + "," + "周日";
                weeks = weeks + "," + "7";
            }
        }

        return flag == 0 ? cycle : weeks;
    }

    public interface SelectRemindWayPopupOnClickListener {
        void obtainMessage(int flag);
    }

    public void setOnSelectRemindWayPopupListener(SelectRemindWayPopupOnClickListener l) {
        this.selectRemindWayPopupListener = l;
    }

    public void dismiss() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    public void showPopup(View rootView) {
        // 第一个参数是要将PopupWindow放到的View，第二个参数是位置，第三第四是偏移值
        mPopupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
    }
}
